package com.hust.chatvedio.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.hust.chatvedio.db.UserDB;
import com.hust.chatvedio.model.User;


public class UserDao {

    private final UserDB mHelper;

    public UserDao(Context context) {
        mHelper = new UserDB(context);
    }

    public void addAccount(User user) {
        //获取数据库对象
        SQLiteDatabase db = mHelper.getReadableDatabase();
        //执行添加操作
        ContentValues values = new ContentValues();
        values.put(UserTable.COL_NAME, user.getName());
        values.put(UserTable.COL_PHOTO, user.getPhoto());

        db.replace(UserTable.TAB_NAME, null, values);
    }

    public User getUserByHxId(String name) {
        //获取数据库对象
        SQLiteDatabase db = mHelper.getReadableDatabase();

        //执行查询语句
        String sql = "select * from " + UserTable.TAB_NAME + " where " + UserTable.COL_NAME + "=?";
        Cursor cursor = db.rawQuery(sql, new String[]{name});
        User user = null;
        if (cursor.moveToNext()) {
            user = new User();

            //封装对象
            user.setName(cursor.getString(cursor.getColumnIndex(UserTable.COL_NAME)));
            user.setPhoto(cursor.getString(cursor.getColumnIndex(UserTable.COL_PHOTO)));
        }
        //关闭资源
        cursor.close();

        //返回数据
        return user;
    }
}
